using System;
using System.IO;
using System.Resources;
using System.Text;
using System.Web.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Util;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web
{
	/// <summary>
	///		Summary description for EditConfigBox.
	/// </summary>
	public class EditConfigBox : UserControl
	{
		private const string passwordPlaceHolder = "########";
		protected Label labelTitle;
		protected Label labelSubtitle;
		protected TextBox textTitle;
		protected TextBox textSubtitle;
		protected RequiredFieldValidator validatorRFTitle;
		protected Label labelContact;
		protected TextBox textContact;
		protected RequiredFieldValidator validatorRFContact;
		protected RegularExpressionValidator validatorREContact;
		protected RequiredFieldValidator validatorRFRoot;
		protected Label labelRoot;
		protected TextBox textRoot;
		protected RegularExpressionValidator validatorRERoot;
		protected ValidationSummary validationSummary;
		protected Label labelCopyright;
		protected TextBox textCopyright;
		protected RequiredFieldValidator RequiredFieldValidator1;
		protected Label labelTheme;
		protected DropDownList listThemes;
		protected Label labelFrontPageCategory;
		protected TextBox textFrontPageCategory;
		protected Label labelFrontPageDayCount;
		protected TextBox textFrontPageDayCount;
		protected RequiredFieldValidator validatorRFFrontPageDayCount;
		protected RangeValidator validatorRGFrontPageDayCount;
		protected Label labelSmtpServer;
		protected CustomValidator validatorSmtpAddress;
		protected CheckBox checkComments;
		protected CheckBox checkTrackbacks;
		protected CheckBox checkPingbacks;
		protected CheckBox checkReferrals;
		protected CheckBox checkPosts;
		protected Label labelPop3Server;
		protected TextBox textPop3Server;
		protected Label labelPop3Username;
		protected TextBox textPop3Username;
		protected TextBox textPop3Password;
		protected TextBox textPop3PasswordRepeat;
		protected CompareValidator validatorCPPop3Password;
		protected Label labelPop3Interval;
		protected Label labelPop3PasswordRepeat;
		protected TextBox textPop3Interval;
		protected RangeValidator validatorRGPop3Interval;
		protected Button buttonSave;
		protected TextBox textSmtpServer;
		protected Label Label1;
		protected TextBox textPop3SubjectPrefix;
		protected CheckBox checkShowCommentCounters;
		protected CheckBox checkEnableAutoPingback;
		protected CheckBox checkEnableCommentApi;
		protected CheckBox checkEnableBloggerApi;
		protected CheckBox checkEnableEditService;
		protected CheckBox checkEnableConfigEditService;
		protected CheckBox checkEnableTrackbackService;
		protected CheckBox checkEnablePingbackService;
		protected CheckBox checkPingWeblogsCom;
		protected CheckBox checkPingBloGs;
		protected Label Label2;
		protected CustomValidator Customvalidator1;
		protected TextBox textXssEndpoint;
		protected TextBox textXssUsername;
		protected CompareValidator Comparevalidator1;
		protected TextBox textXssPassword;
		protected Label Label4;
		protected TextBox textXssPasswordRepeat;
		protected Label Label6;
		protected RangeValidator Rangevalidator1;
		protected TextBox textXssInterval;
		protected Label Label5;
		protected TextBox textXssRssFilename;
		protected CheckBox checkXssEnabled;
		protected CheckBox checkPop3Enabled;
		protected TextBox textNotificationEmailAddress;
		protected RegularExpressionValidator validationRENotification;
		protected CheckBox checkPop3InlineAttachedPictures;
		protected Label Label8;
		protected TextBox textPop3AttachedPicturesPictureThumbnailHeight;
		protected RangeValidator RangeValidator2;
		protected Label Label9;
		protected Label Label10;
		protected CheckBox checkShowItemDescriptionInAggregatedViews;
		protected RequiredFieldValidator RequiredFieldValidator2;
		protected CheckBox checkEnableStartPageCaching;
		protected Label Label11;
		protected DropDownList listTimeZones;
		protected CheckBox checkUseUTC;
		protected Label Label12;
		protected Label lBasicSettings;
		protected Label lServices;
		protected Label lAppearance;
		protected Label lNotifications;
		protected Label lSyndication;
		protected Label lMailToWeblog;
		protected Label lXSS;
		protected CustomValidator validatorPop3Server;
		protected Label labelOtherAppearanceSettings;
		protected CheckBox checkEnableBlogrollDescription;
		protected CheckBox checkEntryTitleAsLink;
		protected Label labelFrontPageEntryCount;
		protected RequiredFieldValidator validatorRFFrontPageEntryCount;
		protected RangeValidator validatorRGFrontPageEntryCount;
		protected TextBox textFrontPageEntryCount;
		protected CheckBox checkEnableComments;
		protected Label labelRssMainDayCount;
		protected TextBox textMainMaxDaysInRss;
		protected RangeValidator validatorRGMainMaxEntriesInRss;
		protected RequiredFieldValidator validatorRFMainMaxEntriesInRss;
		protected Label labelOtherMaxEntriesInRss;
		protected RangeValidator validatorRGOtherMaxEntriesInRss;
		protected RequiredFieldValidator validatorRFOtherMaxEntriesInRss;
		protected TextBox textOtherMaxEntriesInRss;
		protected RangeValidator validatorRGMainMaxDaysInRss;
		protected RequiredFieldValidator validatorRFMainMaxDaysInRss;
		protected Label labelRssMainEntryCount;
		protected TextBox textMainMaxEntriesInRss;
		protected CheckBox checkAlwaysIncludeContentInRSS;
		protected CheckBox checkEnableUrlRewriting;
		protected TextBox textRSSItemFooter;
		protected CheckBox checkEnableRSSItemFooter;
		protected CheckBox checkEnableClickThrough;
		protected CheckBox checkEnableAggregatorBugging;
		protected CheckBox checkEnableCrosspost;
		protected Button buttonTestSMTP;
		protected CheckBox checkCategoryAllEntries;
		protected TextBox textPassword;
		protected TextBox textConfirmPassword;
		protected Label labelTextPassword;
		protected Label labelTextConfirmPassword;
		protected CompareValidator ComparevalidatorPassword;
		protected Label Label3;
		protected Label Label14;
		protected RadioButton mailDeletionAll;
		protected RadioButton mailDeletionProcessed;
		protected CheckBox logIgnoredEmails;
		protected CheckBox logIngnoredEmails;
		protected System.Web.UI.WebControls.Label lBlackList;
		protected System.Web.UI.WebControls.Label Label13;
		protected System.Web.UI.WebControls.TextBox textReferralBlacklist;
		protected System.Web.UI.WebControls.CheckBox checkCaptchaEnabled;
		protected System.Web.UI.WebControls.CheckBox checkReferralBlacklist404s;
		protected System.Web.UI.WebControls.Label labelRSSChannelImage;
		protected System.Web.UI.WebControls.TextBox textRSSChannelImage;
		protected System.Web.UI.WebControls.CheckBox checkReferralUrlBlacklist;
		protected System.Web.UI.WebControls.CheckBox checkMovableTypeBlacklist;
		protected System.Web.UI.WebControls.CheckBox checkEnableTitlePermaLinkSpaces;
		protected System.Web.UI.WebControls.CheckBox checkEnableEncryptLoginPassword;
		protected System.Web.UI.WebControls.Label labelNotificationEmailAddress;
		protected System.Web.UI.WebControls.Label labelSmtpUsername;
		protected System.Web.UI.WebControls.TextBox textSmtpUsername;
		protected System.Web.UI.WebControls.Label labelSmtpPassword;
		protected System.Web.UI.WebControls.TextBox textSmtpPassword;
		protected System.Web.UI.WebControls.CheckBox checkEnableSmtpAuthentication;
		protected System.Web.UI.WebControls.Label labelRssLanguage;
		protected System.Web.UI.WebControls.TextBox textRssLanguage;
		protected System.Web.UI.WebControls.CheckBox checkEnableTitlePermaLinkUnique;
		protected System.Web.UI.WebControls.CheckBox checkEnableTitlePermaLink;
		protected System.Web.UI.WebControls.CheckBox checkEnableSearchHighlight;
		protected System.Web.UI.WebControls.CheckBox checkEnableEntryReferral;
		protected ResourceManager resmgr;

		private void Page_Load(object sender, EventArgs e)
		{
			if (SiteSecurity.IsInRole("admin") == false)
			{
				Response.Redirect("~/FormatPage.aspx?path=SiteConfig/accessdenied.format.html");
			}

			this.ID = "EditConfigBox";

			if (!IsPostBack)
			{
				SharedBasePage requestPage = Page as SharedBasePage;
				SiteConfig siteConfig = requestPage.SiteConfig;
				textContact.Text = siteConfig.Contact;
				textCopyright.Text = siteConfig.Copyright;
				textPassword.Text = passwordPlaceHolder;
				textConfirmPassword.Text = passwordPlaceHolder;
				textFrontPageCategory.Text = siteConfig.FrontPageCategory;
				textFrontPageDayCount.Text = siteConfig.FrontPageDayCount.ToString();
				textFrontPageEntryCount.Text = siteConfig.FrontPageEntryCount.ToString();
				textMainMaxDaysInRss.Text = siteConfig.RssDayCount.ToString();
				textMainMaxEntriesInRss.Text = siteConfig.RssMainEntryCount.ToString();
				textOtherMaxEntriesInRss.Text = siteConfig.RssEntryCount.ToString();
				checkAlwaysIncludeContentInRSS.Checked = siteConfig.AlwaysIncludeContentInRSS;
				checkEnableRSSItemFooter.Checked = siteConfig.EnableRssItemFooters;
				textRSSItemFooter.Text = siteConfig.RssItemFooter;
				checkPop3Enabled.Checked = siteConfig.EnablePop3;
				textPop3Interval.Text = siteConfig.Pop3Interval.ToString();
				textPop3Server.Text = siteConfig.Pop3Server;
				textPop3SubjectPrefix.Text = siteConfig.Pop3SubjectPrefix;
				textPop3Username.Text = siteConfig.Pop3Username;
				textPop3Password.Text = passwordPlaceHolder;
				textPop3PasswordRepeat.Text = passwordPlaceHolder;
				textRoot.Text = siteConfig.Root;
				textSmtpServer.Text = siteConfig.SmtpServer;
				textNotificationEmailAddress.Text = siteConfig.NotificationEMailAddress;
				textSubtitle.Text = siteConfig.Subtitle;
				textSmtpServer.Text = siteConfig.SmtpServer;
				checkComments.Checked = siteConfig.SendCommentsByEmail;
				checkPingbacks.Checked = siteConfig.SendPingbacksByEmail;
				checkReferrals.Checked = siteConfig.SendReferralsByEmail;
				checkPosts.Checked = siteConfig.SendPostsByEmail;
				checkTrackbacks.Checked = siteConfig.SendTrackbacksByEmail;
				checkShowCommentCounters.Checked = siteConfig.ShowCommentCount;
				checkEnableAutoPingback.Checked = siteConfig.EnableAutoPingback;
				checkEnableBloggerApi.Checked = siteConfig.EnableBloggerApi;
				checkEnableComments.Checked = siteConfig.EnableComments;
				checkEnableCommentApi.Checked = siteConfig.EnableCommentApi;
				checkEnableConfigEditService.Checked = siteConfig.EnableConfigEditService;
				checkEnableEditService.Checked = siteConfig.EnableEditService;
				checkEnablePingbackService.Checked = siteConfig.EnablePingbackService;
				checkEnableTrackbackService.Checked = siteConfig.EnableTrackbackService;
				checkEnableClickThrough.Checked = siteConfig.EnableClickThrough;
				checkEnableAggregatorBugging.Checked = siteConfig.EnableAggregatorBugging;
				checkPingWeblogsCom.Checked = siteConfig.NotifyWebLogsDotCom;
				checkPingBloGs.Checked = siteConfig.NotifyBloGs;
				checkXssEnabled.Checked = siteConfig.EnableXSSUpstream;
				textXssEndpoint.Text = siteConfig.XSSUpstreamEndpoint;
				textXssInterval.Text = siteConfig.XSSUpstreamInterval.ToString();
				textXssPassword.Text = passwordPlaceHolder;
				textXssPasswordRepeat.Text = passwordPlaceHolder;
				textXssUsername.Text = siteConfig.XSSUpstreamUsername;
				textXssRssFilename.Text = siteConfig.XSSRSSFilename;
				checkPop3InlineAttachedPictures.Checked = siteConfig.Pop3InlineAttachedPictures;
				textPop3AttachedPicturesPictureThumbnailHeight.Text = siteConfig.Pop3InlinedAttachedPicturesThumbHeight.ToString();
				mailDeletionAll.Checked = siteConfig.Pop3DeleteAllMessages;
				mailDeletionProcessed.Checked = ! siteConfig.Pop3DeleteAllMessages;
				logIgnoredEmails.Checked = siteConfig.Pop3LogIgnoredEmails;
				checkShowItemDescriptionInAggregatedViews.Checked = siteConfig.ShowItemDescriptionInAggregatedViews;
				checkEnableStartPageCaching.Checked = siteConfig.EnableStartPageCaching;
				checkEnableBlogrollDescription.Checked = siteConfig.EnableBlogrollDescription;
				checkEntryTitleAsLink.Checked = siteConfig.EntryTitleAsLink;
				checkEnableUrlRewriting.Checked = siteConfig.EnableUrlRewriting;
				checkEnableCrosspost.Checked = siteConfig.EnableCrossposts;
				checkCategoryAllEntries.Checked = siteConfig.CategoryAllEntries;
				checkReferralUrlBlacklist.Checked = siteConfig.EnableReferralUrlBlackList;
				textReferralBlacklist.Text = siteConfig.ReferralUrlBlackList;
				checkCaptchaEnabled.Checked = siteConfig.EnableCaptcha;
				checkReferralBlacklist404s.Checked = siteConfig.EnableReferralUrlBlackList404s;
				textRSSChannelImage.Text = siteConfig.ChannelImageUrl;
				checkMovableTypeBlacklist.Checked = siteConfig.EnableMovableTypeBlackList;
				checkEnableTitlePermaLink.Checked = siteConfig.EnableTitlePermaLink;
				checkEnableTitlePermaLinkUnique.Checked = siteConfig.EnableTitlePermaLinkUnique;
				checkEnableTitlePermaLinkSpaces.Checked = siteConfig.EnableTitlePermaLinkSpaces;
				checkEnableEncryptLoginPassword.Checked = siteConfig.EncryptLoginPassword;
				checkEnableSmtpAuthentication.Checked = siteConfig.EnableSmtpAuthentication;
				textSmtpUsername.Text = siteConfig.SmtpUserName;
				textSmtpPassword.Text = passwordPlaceHolder;
				textRssLanguage.Text = siteConfig.RssLanguage;
				checkEnableSearchHighlight.Checked = siteConfig.EnableSearchHighlight;
				checkEnableEntryReferral.Checked = siteConfig.EnableEntryReferrals;

				WindowsTimeZoneCollection timeZones = WindowsTimeZone.TimeZones;
				foreach (WindowsTimeZone tz in timeZones)
				{
					listTimeZones.Items.Add(new ListItem(tz.DisplayName, tz.ZoneIndex.ToString()));
				}
				listTimeZones.SelectedValue = siteConfig.DisplayTimeZoneIndex.ToString();
				checkUseUTC.Checked = !siteConfig.AdjustDisplayTimeZone;

				ThemeDictionary themes = Theme.Load(Server.MapPath("themes"));
				foreach (Theme theme in themes.Values)
				{
					listThemes.Items.Add(new ListItem(theme.Title, theme.Name));
				}
				listThemes.SelectedValue = siteConfig.Theme;
				textTitle.Text = siteConfig.Title;

			}
			DataBind();

		}

		#region Web Form Designer generated code

		protected override void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.buttonTestSMTP.Click += new System.EventHandler(this.buttonTestSMTP_Click);
			this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);
			this.Init += new System.EventHandler(this.EditConfigBox_Init);

		}

		#endregion

		private void buttonSave_Click(object sender, EventArgs e)
		{
			if (Page.IsValid)
			{
				SharedBasePage requestPage = Page as SharedBasePage;
				SiteConfig siteConfig = requestPage.SiteConfig;
				siteConfig.Contact = textContact.Text;
				siteConfig.Copyright = textCopyright.Text;
				siteConfig.FrontPageCategory = textFrontPageCategory.Text;
				siteConfig.FrontPageDayCount = int.Parse(textFrontPageDayCount.Text);
				siteConfig.FrontPageEntryCount = int.Parse(textFrontPageEntryCount.Text);
				siteConfig.RssDayCount = int.Parse(textMainMaxDaysInRss.Text);
				siteConfig.RssMainEntryCount = int.Parse(textMainMaxEntriesInRss.Text);
				siteConfig.RssEntryCount = int.Parse(textOtherMaxEntriesInRss.Text);
				siteConfig.AlwaysIncludeContentInRSS = checkAlwaysIncludeContentInRSS.Checked;
				siteConfig.EnableRssItemFooters = checkEnableRSSItemFooter.Checked;
				siteConfig.RssItemFooter = textRSSItemFooter.Text;
				siteConfig.EnablePop3 = checkPop3Enabled.Checked;
				siteConfig.Pop3Interval = int.Parse(textPop3Interval.Text);
				siteConfig.Pop3Server = textPop3Server.Text;
				siteConfig.Pop3SubjectPrefix = textPop3SubjectPrefix.Text;
				siteConfig.Pop3Username = textPop3Username.Text;
				if (textPop3Password.Text.Length > 0 &&
					textPop3Password.Text != passwordPlaceHolder)
				{
					siteConfig.Pop3Password = textPop3Password.Text;
				}
				siteConfig.Pop3DeleteAllMessages = mailDeletionAll.Checked;
				siteConfig.Pop3LogIgnoredEmails = logIgnoredEmails.Checked;
				siteConfig.EnableXSSUpstream = checkXssEnabled.Checked;
				siteConfig.XSSUpstreamEndpoint = textXssEndpoint.Text;
				siteConfig.XSSUpstreamInterval = int.Parse(textXssInterval.Text);
				if (textXssPassword.Text.Length > 0 &&
					textXssPassword.Text != passwordPlaceHolder)
				{
					siteConfig.XSSUpstreamPassword = textXssPassword.Text;
				}
				siteConfig.XSSUpstreamUsername = textXssUsername.Text;
				siteConfig.XSSRSSFilename = textXssRssFilename.Text;

				siteConfig.Root = textRoot.Text;
				siteConfig.SmtpServer = textSmtpServer.Text;
				siteConfig.NotificationEMailAddress = textNotificationEmailAddress.Text;
				siteConfig.SendCommentsByEmail = checkComments.Checked;
				siteConfig.SendPingbacksByEmail = checkPingbacks.Checked;
				siteConfig.SendReferralsByEmail = checkReferrals.Checked;
				siteConfig.SendTrackbacksByEmail = checkTrackbacks.Checked;
				siteConfig.SendPostsByEmail = checkPosts.Checked;
				siteConfig.EnableAutoPingback = checkEnableAutoPingback.Checked;
				siteConfig.EnableBloggerApi = checkEnableBloggerApi.Checked;
				siteConfig.EnableComments = checkEnableComments.Checked;
				siteConfig.EnableCommentApi = checkEnableCommentApi.Checked;
				siteConfig.EnableConfigEditService = checkEnableConfigEditService.Checked;
				siteConfig.EnableEditService = checkEnableEditService.Checked;
				siteConfig.EnableTrackbackService = checkEnableTrackbackService.Checked;
				siteConfig.EnablePingbackService = checkEnableTrackbackService.Checked;
				siteConfig.EnableClickThrough = checkEnableClickThrough.Checked;
				siteConfig.EnableAggregatorBugging = checkEnableAggregatorBugging.Checked;
				siteConfig.NotifyWebLogsDotCom = checkPingWeblogsCom.Checked;
				siteConfig.NotifyBloGs = checkPingBloGs.Checked;
				siteConfig.Subtitle = textSubtitle.Text;
				siteConfig.Title = textTitle.Text;
				siteConfig.ShowCommentCount = checkShowCommentCounters.Checked;
				siteConfig.Pop3InlineAttachedPictures = checkPop3InlineAttachedPictures.Checked;
				siteConfig.Pop3InlinedAttachedPicturesThumbHeight = int.Parse(textPop3AttachedPicturesPictureThumbnailHeight.Text);
				siteConfig.ShowItemDescriptionInAggregatedViews = checkShowItemDescriptionInAggregatedViews.Checked;
				siteConfig.EnableStartPageCaching = checkEnableStartPageCaching.Checked;
				siteConfig.EnableBlogrollDescription = checkEnableBlogrollDescription.Checked;
				siteConfig.EnableUrlRewriting = checkEnableUrlRewriting.Checked;
				siteConfig.DisplayTimeZoneIndex = Convert.ToInt32(listTimeZones.SelectedValue);
				siteConfig.AdjustDisplayTimeZone = !checkUseUTC.Checked;
				siteConfig.EntryTitleAsLink = checkEntryTitleAsLink.Checked;
				siteConfig.EnableCrossposts = checkEnableCrosspost.Checked;
				if (textPassword.Text.Length > 0 &&
					textPassword.Text != passwordPlaceHolder)
				{
					SiteSecurity.SetPassword(requestPage.User.Identity.Name, textPassword.Text);
				}
				siteConfig.CategoryAllEntries = checkCategoryAllEntries.Checked;
				requestPage.UserTheme = siteConfig.Theme = listThemes.SelectedValue;

				siteConfig.ReferralUrlBlackList = textReferralBlacklist.Text.TrimEnd(';');
				siteConfig.EnableReferralUrlBlackList = checkReferralUrlBlacklist.Checked;
				siteConfig.EnableCaptcha = checkCaptchaEnabled.Checked;
				siteConfig.EnableReferralUrlBlackList404s = checkReferralBlacklist404s.Checked;
				siteConfig.ChannelImageUrl = textRSSChannelImage.Text;
				siteConfig.EnableMovableTypeBlackList = checkMovableTypeBlacklist.Checked;
				siteConfig.EnableTitlePermaLink = checkEnableTitlePermaLink.Checked;
				siteConfig.EnableTitlePermaLinkUnique = checkEnableTitlePermaLinkUnique.Checked;
				siteConfig.EnableTitlePermaLinkSpaces = checkEnableTitlePermaLinkSpaces.Checked;
				siteConfig.EncryptLoginPassword = checkEnableEncryptLoginPassword.Checked;
				siteConfig.EnableSmtpAuthentication = checkEnableSmtpAuthentication.Checked;
				siteConfig.SmtpUserName = textSmtpUsername.Text;
				if (textSmtpPassword.Text.Length > 0 &&
					textSmtpPassword.Text != passwordPlaceHolder)
				{
					siteConfig.SmtpPassword = textSmtpPassword.Text;
				}
				siteConfig.RssLanguage = textRssLanguage.Text;
				siteConfig.EnableSearchHighlight = checkEnableSearchHighlight.Checked;
				siteConfig.EnableEntryReferrals = checkEnableEntryReferral.Checked;

				SiteConfig.Save(siteConfig);

				if (siteConfig.EnableMovableTypeBlackList)
				{
					ReferralBlackListFactory.AddBlacklist(new MovableTypeBlacklist(), Path.Combine(SiteConfig.GetConfigPathFromCurrentContext(), "blacklist.txt"));
				}
				else
				{
					ReferralBlackListFactory.RemoveBlacklist(typeof(MovableTypeBlacklist));
				}

				if (siteConfig.EnableReferralUrlBlackList && siteConfig.ReferralUrlBlackList.Length != 0)
				{
					ReferralBlackListFactory.AddBlacklist(new ReferralUrlBlacklist(), siteConfig.ReferralUrlBlackList);
				}
				else
				{
					ReferralBlackListFactory.RemoveBlacklist(typeof(ReferralUrlBlacklist));
				}

				requestPage.Redirect(Page.Request.Url.AbsoluteUri);
			}
		}

		private void EditConfigBox_Init(object sender, EventArgs e)
		{
			resmgr = ApplicationResourceTable.Get();
		}

		private void buttonTestSMTP_Click(object sender, EventArgs e)
		{
			SiteConfig siteConfig = SiteConfig.GetSiteConfig();

			if (textSmtpServer.Text != "" & textNotificationEmailAddress.Text != "")
			{
				MailMessage emailMessage = new MailMessage();

				emailMessage.To = textNotificationEmailAddress.Text;
				emailMessage.Subject = String.Format("dasBlog test message");
				emailMessage.Body = String.Format("This is a test message from dasBlog. If you are reading this then everything is working properly.");
				emailMessage.BodyFormat = MailFormat.Text;
				emailMessage.BodyEncoding = Encoding.UTF8;
				emailMessage.From = siteConfig.Contact;
				SendMailInfo sendMailInfo = new SendMailInfo(emailMessage, textSmtpServer.Text, 
					checkEnableSmtpAuthentication.Checked, textSmtpUsername.Text, textSmtpPassword.Text);

				try
				{
					// RyanG: Added support for authentication on outgoing smtp
					MailMessage message = sendMailInfo.Message;
					if (sendMailInfo.EnableSmtpAuthentication) 
					{
						message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);	// enable basic authentication
						message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", sendMailInfo.SmtpUserName);	// set the username
						message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendMailInfo.SmtpPassword);  // set the password
					}

					SmtpMail.SmtpServer = sendMailInfo.SmtpServer;
					SmtpMail.Send(message);
				}
				catch (Exception ex)
				{
					//RyanG: Decode the real reason the error occured by looking at the inner exceptions
					System.Text.StringBuilder exceptionMessage = new System.Text.StringBuilder();
					Exception lastException = ex;
					while (lastException != null) 
					{
						if (exceptionMessage.Length > 0)
							exceptionMessage.Append("; ");
						exceptionMessage.Append(lastException.Message);
						lastException = lastException.InnerException;
					}

					ILoggingDataService logService = LoggingDataServiceFactory.GetService(Server.MapPath(siteConfig.LogDir));
					logService.AddEvent(
						new EventDataItem(EventCodes.SmtpError, "", exceptionMessage.ToString()));

					Response.Redirect("FormatPage.aspx?path=SiteConfig/pageerror.format.html", true);
				}
			}
		}
	}
}